package com.verifone.tony.databasetest;

import android.content.Context;
import android.support.annotation.NonNull;

import com.raizlabs.android.dbflow.config.FlowConfig;
import com.raizlabs.android.dbflow.config.FlowManager;
import com.raizlabs.android.dbflow.sql.language.Select;
import com.raizlabs.android.dbflow.structure.ModelAdapter;

/**
 * Created by chong.z on 2017/11/27.
 */

public class DBFunction {
    //下面以方法的方式测dbflow, 测试代码在androidTest
    public static void dbInit(@NonNull Context context) {
        //用dbFlowModel前必须执行这个
        FlowManager.init(new FlowConfig.Builder(context).build());
    }

    //put
    public static void db1Add(@NonNull DBFlowModel dbFlowModel) {
        DBFlowModel mDBFlowModel = new Select().from(DBFlowModel.class).where(DBFlowModel_Table.modelname.is(dbFlowModel.getModelname())).querySingle();

        //表里没有，新建
        if (mDBFlowModel == null) {
            dbFlowModel.save();
        } else { //表里有，更新
            ModelAdapter<DBFlowModel> adapter = FlowManager.getModelAdapter(DBFlowModel.class);
            dbFlowModel.id = mDBFlowModel.id;
            adapter.update(dbFlowModel);
        }
    }

    //delete
    public static void db1Del(@NonNull String transID) {
        DBFlowModel dbFlowModel = new Select().from(DBFlowModel.class).where(DBFlowModel_Table.modelname.is(transID)).querySingle();
        if (dbFlowModel != null) {
            dbFlowModel.delete();
        }
    }

    //改
/*    public static void db1Update(@NonNull DBFlowModel dbFlowModel) {
        ModelAdapter<DBFlowModel> adapter = FlowManager.getModelAdapter(DBFlowModel.class);
        dbFlowModel.id = 1;
        adapter.update(dbFlowModel);
    }*/

    //get
    public static DBFlowModel db1Get(String transID) {
        return new Select().from(DBFlowModel.class).where(DBFlowModel_Table.modelname.is(transID)).querySingle();
    }
}
